package com.ayoka.parenting.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.ImageBean;
import com.ayoka.parenting.bean.TopicBean;
import com.ayoka.parenting.widget.CommentListView;
import com.ayoka.parenting.widget.ExpandTextView;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.utils.StringUtil;
import com.jincp.common.utils.UIUtils;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;
import com.jincp.image.showImage.ShowPictureActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by jinchunping on 2018/4/11.
 */

public class CircleGroupTopicListAdapter extends BaseQuickAdapter<TopicBean, BaseViewHolder> {

    private String circleGroupOwnerId;
    private String newPublishTopicId;


    public CircleGroupTopicListAdapter(String circleGroupOwnerId) {
        super(R.layout.item_circle_group_topic_list);
        this.circleGroupOwnerId = circleGroupOwnerId;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final TopicBean item) {
        if (getRecyclerView() != null && TextUtils.equals(item.Id, newPublishTopicId)) {
            getRecyclerView().smoothScrollToPosition(holder.getLayoutPosition());
        }
        // 设置案主信息
        if (item.UserInfo != null) {
            ImageLoader.getInstance().displayImage(item.UserInfo.LocalAvatar, (ImageView) holder.getView(R.id.iv_publish_header), ImageLoaderOptions.getCircleOptions());
            holder.setText(R.id.tv_publish_name, item.UserInfo.WeixinNickName)
                    .setText(R.id.tv_publish_date, item.CreateDate);
        }

        // 设置操作按钮  圈主和本人才能展示
        holder.setVisible(R.id.btn_operate, TopicBean.isCircleGroupOwnerId(circleGroupOwnerId) ||TopicBean.isTopicOwner(item.Uid))
                .addOnClickListener(R.id.btn_operate);


        // 设置文本消息
        final ExpandTextView expandTextView = holder.getView(R.id.etv_content);
        expandTextView.setExpand(item.isExpanded);
        if (!TextUtils.isEmpty(item.Content)) {
            expandTextView.setExpandStatusListener(new ExpandTextView.ExpandStatusListener() {
                @Override
                public void statusChange(boolean isExpanded) {
                    item.isExpanded = isExpanded;
                }
            });
            expandTextView.setText(StringUtil.formatUrlString(item.Content));
        }
        expandTextView.setVisibility(TextUtils.isEmpty(item.Content) ? View.GONE : View.VISIBLE);
        holder.addOnClickListener(expandTextView.getTvContent().getId());

        // 设置不同的数据类型
        LinearLayout layoutContentContainer = holder.getView(R.id.layout_content_container);
        layoutContentContainer.removeAllViews();
        configMediaTypeView(item, layoutContentContainer);
        holder.addOnClickListener(R.id.layout_content_container);

        // 设置评论和点赞
        holder.setText(R.id.tv_comment, String.valueOf(item.CommentCount))
                .setText(R.id.tv_praise, String.valueOf(item.LikeCount))
                .setSelected(R.id.tv_praise, item.IsLike == 1)
                .addOnClickListener(R.id.tv_comment)
                .addOnClickListener(R.id.tv_praise);

        // 设置评论列表
        CommentListView commentList = holder.getView(R.id.commentList);
        if (item.Comments == null || item.Comments.isEmpty()) {
            // 设置评论layout不显示
            holder.setVisible(R.id.ll_comment_container, false);
        } else {
            holder.setVisible(R.id.ll_comment_container, true);
            commentList.setDatas(item.Comments, false);
        }
    }

    private void configMediaTypeView(TopicBean item, ViewGroup container) {
        switch (item.getType()) {
            case TopicBean.TYPE_URL:// 处理链接动态的链接内容和和图片
                break;
            case TopicBean.TYPE_IMAGE:// 处理图片
                if (item.Images != null && !item.Images.isEmpty()) {
                    View imageContainer = View.inflate(mContext, R.layout.layout_image_body, container);
                    RecyclerView recyclerView = imageContainer.findViewById(R.id.recyclerView_picture);
                    recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
                    BaseQuickAdapter<ImageBean, BaseViewHolder> adapter = new BaseQuickAdapter<ImageBean, BaseViewHolder>(R.layout.item_image_item) {
                        @Override
                        protected void convert(BaseViewHolder holder, ImageBean item) {
                            ImageLoader.getInstance().displayImage(item.SmallUrl, (ImageView) holder.getView(R.id.iv_picture));
                        }
                    };
                    adapter.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            ArrayList<String> photoUrls = new ArrayList<>();
                            for (ImageBean imageBean : (List<ImageBean>)adapter.getData()) {
                                // 查看图片中显示bigImage
                                photoUrls.add(imageBean.URL);
                            }
                            ShowPictureActivity.start(mContext, photoUrls, position, new ShowPictureActivity.ImageSize(UIUtils.getScreenWidth(), UIUtils.getScreenHeight()));
                        }
                    });
                    adapter.setNewData(item.Images);
                    recyclerView.setAdapter(adapter);
                }
                break;
            case TopicBean.TYPE_AUDIO:// 处理音频
                break;
            case TopicBean.TYPE_VIDEO:// 处理视频
                break;
            default:
                break;
        }
    }

    public void setNewPublishTopicId(String newPublishTopicId) {
        this.newPublishTopicId = newPublishTopicId;
    }
}
